<?php

class WsluckyboxControl extends Control
{

	private $uid = NULL;
	private $luck_lv = array();
	private $com = array();

	public function __construct()
	{
		parent::__construct();
		load_lib('common');
		$uid = snscall('getUID', array());
		if (!__is_big_num($uid))
		{
			$this->out_error(LOGIN_EXPIRE_ERROR_CODE);
			return FALSE;
		}
		$this->uid = $uid;
		CFG::getInstance()->load_config('prop');
		CFG::getInstance()->load_config('wsluckybox');
		$this->luck_lv = CFG::getInstance()->config['wsluckybox']['luck_lv'];
		$this->com = CFG::getInstance()->config['wsluckybox']['com'];
	}

	public function __destruct()
	{
		parent::__destruct();
	}

	//init面板
	public function initlucky()
	{
		
	}
	
	//开宝箱
	public function luckytime()
	{
		$uid = $this->uid;
		if (!$uid)
			return FALSE;
		//获取幸运度------------------------------
		$luck_val = 10;
		//------------------------------------------
		$first_luck_lv = $this->get_first_luck_lv($luck_val);
		##########
		#  first_rand #
		##########
		$first_luck_lv = CFG::getInstance()->config['wsluckybox'][$first_luck_lv];
		if (!is_array($first_luck_lv))
		{
			return FALSE; //取配置错误
		}
		//---------------------
		$box = maocall("prop", "getCommProp", array($uid));
		$box_num = $box["宝箱"];
		//---------------------
		if (empty($_POST['num']))
		{
			$luckbox = 1;
		} else
		{
			$luckbox = $_POST['num'];
		}
		if ($box_num < $luckbox || empty($box_num))
		{
			$this->out_error(10001); //宝箱数量不够
			return FALSE;
		}
		for ($i = 0; $i < $luckbox; $i++)
		{
			$hours = date("G:i");
			$first_val = $this->custom_rand($first_luck_lv, 1);
			if ("com" == $first_val)
			{
				$list = array();
				##########
				# com_rand #
				##########
				$com = $this->com;
				if (!is_array($com))
				{
					return FALSE; //取配置错误
				}
				$prop = $this->custom_rand($com);
				$p_id = $prop[0];
				$p_num = $prop[1];
				$list[$p_id] += $p_num;
				$addprop = $this->__addProp($uid, $list);
				if ($addprop)
				{
					if ("1" == $prop[2])
					{
						//进喇叭库
						$go_to_cache=$hours."-".$uid."-".$p_id."-".$p_num;
					}
					//进个人库
				}
			} elseif ("sp" == $first_val)
			{
				$p_id="";
				$p_num=1;
				$addprop = apicall("prop", "addUserProp", array($uid, $p_id, $p_num));
				if ($addprop)
				{
					//进喇叭库
					//进个人库
					$go_to_cache=$hours."-".$uid."-".$p_id."-".$p_num;
				}
			} else
			{
				$this->out_error(10001); //第一次rand出错
				return FALSE;
			}
		}
		$sub=apicall("prop", "subUserProp", array($uid, "宝箱", 1));
		if($sub)
		{
			
		}
	}

	//我的获奖详情
	public function getinfo()
	{
		
	}
	
	//领取幸运箱
	public function getbox()
	{
		
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////	

	/**
	 * 计算first_luck_lv等级
	 * @param type $luck_val	输入当前幸运度
	 * @return $value		输出对应幸运等级
	 */
	private function get_first_luck_lv($luck_val)
	{
		$luck_lv = $this->luck_lv;
		if (is_array($luck_lv))
		{
			foreach ($luck_lv as $key => $value)
			{
				if ($luck_val <= $key)
				{
					return $value;
				}
			}
			$value = "first_luck_lv6";
			return $value;
		}
		return FALSE;
	}

	/**
	 * 自定义RAND
	 * @param type $arr
	 * @param type $type	默认为0。。0：千位数rand，1：万为数rand
	 * @return $val 
	 */
	private function custom_rand($arr, $type=0)
	{
		if ("0" == $type)
		{
			$num = mt_rand(1, 1000);
		} elseif ("1" == $type)
		{
			$num = mt_rand(1, 10000);
		}
		foreach ($arr as $key => $val)
		{
			if ($num <= $key)
			{
				return $val;
			}
		}
		return FALSE;
	}

	/**
	 * 加道具
	 * @param type $uid
	 * @param type $props
	 * @return bool 
	 */
	private function __addProp($uid, $props = array())
	{
		if (empty($props) || !is_array($props))
		{
			return false;
		}
		$propInfo = array();
		foreach ($props as $prop_id => $nums)
		{
			if ($prop_id === 0)
			{
				$ret = apicall('user', 'addNum', array('gb', $uid, $nums));
				if (!$ret)
				{
					$ret = apicall('user', 'addNum', array('gb', $uid, $nums));
				}
			} else if ($prop_id == '9998')
			{
				$ret = maocall('user', 'addUserDiamond', array($uid, $nums));
				if (!$ret)
				{
					$ret = maocall('user', 'addUserDiamond', array($uid, $nums));
				}
			} elseif (is_numeric($prop_id) && $prop_id > 0)
			{
				$propInfo[$prop_id] = $nums;
			}
		}

		if (!empty($propInfo))
		{
			$ret = apicall("prop", "addUserProp", array($uid, $prop_id, $nums));
			if (!$ret)
			{
				$ret = apicall("prop", "addUserProp", array($uid, $prop_id, $nums));
			}
		}
		if ($ret)
			return true;
		else
			return false;
	}

}
?>

